#include<stdio.h>

int main(int argc, char *argv[])
{
	int year;
	int month;
	int i;
	int days;
	int monthdays;
	int week;

	do{
		printf("欢迎使用日历系统！\n请输入年份月份(空格分隔)：");
		scanf("%d%d", &year, &month);
	}while(!(year>=1970&&month>=1&&month<=12));

	days = 0;
	for(i = 1970;i<year;i++){
		if((i%4==0&&i%100!=0)||(i%400==0)){
			days+=366;
		}else{
			days+=365;
		}
	}

	for(i = 1 ; i < month ; i++){
		if(1==i || 3==i || 5==i || 7==i  || 8==i || 10==i || 12 ==i){
			days+=31;
		}else if(2==i){
			if((year%4==0&&year%100!=0)||(year%400==0)){
				days+=29;
			}else{
				days+=28;
			}
		}else{
			days+=30;
		}
	}
	days += 1;//year month 1号距离1970-1-1多少天；
	week = (days + 3) % 7;
	if(0 == week){
		week = 7;
	}

	printf("%d年的%d月1日是周%d\n", year, month, week);


	if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month || 12 == month)
		monthdays = 31;
	else if (4 == month || 6 == month || 9 == month || 11 == month)
		monthdays = 30;
	else
		monthdays = (28 + (year % 4 == 0 && year % 100 != 0 || year % 400 == 0));
	printf("      %d月 %d\n", month, year);
	printf("日 一 二 三 四 五 六\n");
	for(i = 0; i < week;i++){
		printf("   ");
	}
	for(i = 1; i <= monthdays; i++){
		printf("%2d%c", i, (i+week)%7 ? ' ' : '\n');
	}

	printf("\n");
	return 0;
}
